<template>
  <div class="user-modal" :class="{ active: visible }" @click.self="close">
    <div class="user-container">
      <div class="user-header">
        <span>设置用户信息</span>
        <button class="close-btn" @click="close">
          <img src="@/assets/icons/close.svg" alt="关闭">
        </button>
      </div>

      <div class="user-content">
        <div class="avatar-section">
          <div class="avatar-preview">
            <img :src="avatarPreview" alt="用户头像" class="preview-image">
            <div class="avatar-overlay" v-if="isCustomAvatar">
              <span>自定义头像</span>
            </div>
          </div>

          <div class="avatar-options">
            <div
                v-for="(avatar, index) in avatarOptions"
                :key="index"
                class="avatar-option"
                :class="{ selected: selectedAvatar === avatar }"
                @click="selectAvatar(avatar)"
            >
              <img :src="avatar" alt="头像选项">
            </div>
          </div>
        </div>

        <div class="form-section">
          <div class="input-group">
            <label for="nickname">昵称</label>
            <input
                type="text"
                id="nickname"
                v-model="nickname"
                placeholder="请输入昵称（2-12个字符）"
                maxlength="12"
                @input="validateNickname"
            >
            <div class="error-message" v-if="errorMessage">{{ errorMessage }}</div>
          </div>
          <div class="input-group">
            <label for="nickname">自定义头像</label>
            <input
                type="text"
                id="nickname"
                v-model="customAvatarUrl"
                placeholder="输入图片URL"
                maxlength="128"
                @input="updateCustomAvatar"
            >
          </div>

          <div class="actions">
            <button class="cancel-btn" @click="close">取消</button>
            <button class="confirm-btn" @click="confirm" :disabled="!isFormValid">保存设置</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import {setUserInfo, getUserInfo} from "@/api";
import {ref, computed} from 'vue'
import BOY_AVATAR_B from '@/assets/avatar/BOY_AVATAR_B.svg'
import GIRL_AVATAR_A from '@/assets/avatar/GIRL_AVATAR_A.svg'
import GIRL_AVATAR_B from '@/assets/avatar/GIRL_AVATAR_B.svg'
import BOY_AVATAR_A from '@/assets/avatar/BOY_AVATAR_A.svg'

// 控制弹窗显示
const visible = ref(false)

// 用户输入
const nickname = ref('')
const selectedAvatar = ref(BOY_AVATAR_A)
const customAvatarUrl = ref('')
const showUrlInput = ref(false)
const errorMessage = ref('')
const score = ref(0)

const avatarOptions = ref([
  BOY_AVATAR_B,
  GIRL_AVATAR_A,
  GIRL_AVATAR_B,
  BOY_AVATAR_A
])

const isCustomAvatar = computed(() => {
  return selectedAvatar.value === customAvatarUrl.value && customAvatarUrl.value !== ''
})

const avatarPreview = computed(() => {
  if (isCustomAvatar.value && customAvatarUrl.value) {
    return customAvatarUrl.value
  }
  return selectedAvatar.value
})

const isFormValid = computed(() => {
  return nickname.value.trim().length >= 2 && !errorMessage.value
})

const selectAvatar = (avatar: string) => {
  selectedAvatar.value = avatar
  showUrlInput.value = false
  customAvatarUrl.value = ''
}

const updateCustomAvatar = () => {
  if (customAvatarUrl.value.trim() !== '') {
    selectedAvatar.value = customAvatarUrl.value
  }
}

const validateNickname = () => {
  const trimmedNickname = nickname.value.trim()

  if (!trimmedNickname) {
    errorMessage.value = '昵称不能为空'
    return false
  }

  if (trimmedNickname.length < 2) {
    errorMessage.value = '昵称至少需要2个字符'
    return false
  }

  if (trimmedNickname.length > 12) {
    errorMessage.value = '昵称不能超过12个字符'
    return false
  }

  errorMessage.value = ''
  return true
}

const open = async () => {
  visible.value = true
  document.body.style.overflow = 'hidden'
  try {
    const res = await getUserInfo()
    nickname.value = res.data.nickname
    selectedAvatar.value = res.data.avatar
    score.value = res.data.score
  } finally {

  }
}

const close = () => {
  visible.value = false
  document.body.style.overflow = ''

  // 重置表单
  nickname.value = ''
  selectedAvatar.value = BOY_AVATAR_A
  customAvatarUrl.value = ''
  showUrlInput.value = false
  errorMessage.value = ''
}

/**
 * 提交表单
 */
const confirm = async () => {
  if (validateNickname()) {
    await setUserInfo({
      nickname: nickname.value,
      avatar: selectedAvatar.value,
    })
    close()
  }
}

defineExpose({
  open,
  close
})
</script>

<style scoped>
.user-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgba(0, 0, 0, 0.7);
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

.user-modal.active {
  opacity: 1;
  pointer-events: all;
}

.user-container {
  max-width: 500px;
  width: 95%;
  border-radius: 10px;
  overflow: hidden;
  background-color: #fff;
  transform: scale(0.9);
  transition: transform 0.3s ease;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

.user-modal.active .user-container {
  transform: scale(1);
}

.user-header {
  padding: 15px 20px;
  text-align: left;
  color: #1f1f1f;
  position: relative;
  font-size: 18px;
  border-bottom: 1px solid #eee;
  background-color: #fff;
}

.close-btn {
  position: absolute;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 5px;
  border-radius: 50%;
  transition: background-color 0.2s;
}

.close-btn:hover {
  background-color: #f0f0f0;
}

.close-btn img {
  width: 20px;
  height: 20px;
}

.user-content {
  padding: 25px;
  display: flex;
  flex-direction: column;
  gap: 25px;
}

.avatar-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

.avatar-preview {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  overflow: hidden;
  border: 3px solid #0081ff;
  box-shadow: 0 0 0 3px rgba(0, 129, 255, 0.2);
  position: relative;
}

.preview-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.avatar-overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  color: #fff;
  background-color: #0081ff;
  text-align: center;
  padding: 5px;
  font-size: 12px;
}

.avatar-options {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  justify-content: center;
  max-width: 300px;
}

.avatar-option {
  width: 55px;
  height: 55px;
  border-radius: 50%;
  overflow: hidden;
  cursor: pointer;
  border: 2px solid transparent;
  transition: all 0.3s ease;
  position: relative;
}

.avatar-option:hover {
  transform: scale(1.1);
  border-color: #0081ff;
  box-shadow: 0 0 0 3px rgba(0, 129, 255, 0.2);
}

.avatar-option.selected {
  border-color: #0081ff;
  box-shadow: 0 0 0 3px rgba(0, 129, 255, 0.2);
}

.avatar-option img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.form-section {
  display: flex;
  flex-direction: column;
  gap: 25px;
}

.input-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.input-group label {
  font-size: 15px;
  color: #555;
  font-weight: 500;
}

.input-group input {
  padding: 10px 15px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 16px;
  transition: all 0.3s;
}

.input-group input:focus {
  outline: none;
  border-color: #0081ff;
  box-shadow: 0 0 0 3px rgba(0, 129, 255, 0.2);
}

.error-message {
  color: #e74c3c;
  font-size: 13px;
}

.actions {
  display: flex;
  gap: 15px;
  justify-content: flex-end;
  margin-top: 10px;
}

.cancel-btn {
  border-radius: 5px;
  padding: 8px 40px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s;
  background-color: #fff;
  color: #555;
  border: 1px solid #ddd;
}

.cancel-btn:hover {
  background-color: #eaeaea;
}

.confirm-btn {
  background: linear-gradient(to right, #0081ff, #0066cc);
  color: white;
  border: none;
  border-radius: 5px;
  padding: 8px 40px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: 0 4px 12px rgba(0, 129, 255, 0.3);
}

.confirm-btn:hover {
  box-shadow: 0 6px 15px rgba(0, 129, 255, 0.4);
}

.confirm-btn:disabled {
  background: #bdc3c7;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

@media (max-width: 480px) {
  .user-container {
    width: 95%;
  }

  .user-content {
    padding: 20px;
  }

  .avatar-preview {
    width: 100px;
    height: 100px;
  }

  .avatar-option {
    width: 50px;
    height: 50px;
  }

  .actions {
    flex-direction: column;
    gap: 10px;
  }

  .cancel-btn, .confirm-btn {
    width: 100%;
    padding: 12px;
  }
}
</style>